//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "SettingsForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cspin"
#pragma resource "*.dfm"
TSF *SF;
//---------------------------------------------------------------------------
void TSF::SaveSettings() {
	UnicodeString name;
	for (int i = 0; i < p.size(); i++) {
		name = "[" + IntToStr(i) + "]  " + p0[i].name;
		MF->pn->setPosNameDesc(i, p[i].name, p[i].des);
		MF->pn->setMarkCount(name, p[i].mc);
	}
	for (int i = 0; i < t.size(); i++) {
		name = "[" + IntToStr(i) + "]  " + t0[i].name;
		MF->pn->setTransitionForm(name, t[i].form);
		MF->pn->setTrnNameDesc(i, t[i].name, t[i].des);
	}
	MF->pn->refrash();
	MF->pn->changePositionList(MF->PositionCB);
	MF->pn->changeTransitionList(MF->TransitionCB);
}
//---------------------------------------------------------------------------
void TSF::Clear() {
	PosChB->Enabled = false;
	CountSE->Enabled = false;
	DPosM->Enabled = false;
	PosNE->Text = "";
	NPosCB->Text = "";
	DPosM->Clear();
	CountSE->Value = 0;
	TrnNE->Text = "";
	NTrnCB->Text = "";
	DTrnM->Clear();
	FormChB->Enabled = false;
	StChB->Enabled = false;
	DTrnM->Enabled = false;
}
//---------------------------------------------------------------------------
__fastcall TSF::TSF(TComponent* Owner) : TForm(Owner) {}
//---------------------------------------------------------------------------
void TSF::changePositionList() {
	NPosCB->Clear();
	for (int i = 0; i < p.size(); i++)
		NPosCB->AddItem(p[i].name, NPosCB);
}
//---------------------------------------------------------------------------
void TSF::changeTransitionList() {
	NTrnCB->Clear();
	for (int i = 0; i < t.size(); i++)
		NTrnCB->AddItem(t[i].name, NTrnCB);
}
//---------------------------------------------------------------------------
void __fastcall TSF::NPosCBChange(TObject *Sender) {
	int index = NPosCB->ItemIndex;
	if (index != -1) {
		DPosM->Clear();
		PosNE->Text = index;
		PosChB->Checked = false;
		if (p[index].mc == 100) {
			CountSE->Enabled = false;
			PosChB->Checked = true;
		}
		else {
			CountSE->Enabled = true;
			CountSE->Value = p[index].mc;
		}
		PosChB->Enabled = true;
		DPosM->Enabled = true;
		if (p[index].des != "")
			DPosM->Lines->Add(p[index].des);
	}
}
//---------------------------------------------------------------------------
void __fastcall TSF::NTrnCBChange(TObject *Sender) {
	int index = NTrnCB->ItemIndex;
	if (index != -1) {
		DTrnM->Clear();
		TrnNE->Text = index;
		FormChB->Enabled = true;
		FormChB->Checked = t[index].form;
		StChB->Checked = t[index].state;
		DTrnM->Enabled = true;
		if (t[index].des != "")
			DTrnM->Lines->Add(t[index].des);
	}
}
//---------------------------------------------------------------------------
void __fastcall TSF::FormShow(TObject *Sender) {
	posinf pos;
	trninf trn;
	UnicodeString name;
	p.clear();
	t.clear();
	p0.clear();
	t0.clear();
	for (int i = 0; i < NPosCB->Items->Count; i++) {
		pos.name = NPosCB->Items->Strings[i];
		name = "[" + IntToStr(i) + "]  " + pos.name;
		pos.mc = MF->pn->getMarkCount(name);
		pos.des = MF->pn->getDescription(name);
		p.push_back(pos);
		p0.push_back(pos);
	}
	for (int i = 0; i < NTrnCB->Items->Count; i++) {
		trn.name = NTrnCB->Items->Strings[i];
		name = "[" + IntToStr(i) + "]  " + trn.name;
		trn.form = MF->pn->getTransitionForm(name);
		trn.state = MF->pn->getTransitionState(name);
		trn.des = MF->pn->getDescription(name);
		t.push_back(trn);
		t0.push_back(trn);
	}
}
//---------------------------------------------------------------------------
void __fastcall TSF::NPosCBKeyPress(TObject *Sender, wchar_t &Key) {
	if (Key == '\r') {
		if (Sender == NPosCB) {
			p[StrToInt(PosNE->Text)].name = NPosCB->Text;
			changePositionList();
			NPosCB->ItemIndex = StrToInt(PosNE->Text);
		}
		if (Sender == NTrnCB) {
			t[StrToInt(TrnNE->Text)].name = NTrnCB->Text;
			changeTransitionList();
			NTrnCB->ItemIndex = StrToInt(TrnNE->Text);
		}
	}
}
//---------------------------------------------------------------------------
void __fastcall TSF::CountSEChange(TObject *Sender) {
	if (NPosCB->Text != "" && CountSE->Text != "") {
		if (CountSE->Value > 99)
			CountSE->Value = 99;
		if (CountSE->Enabled)
			p[StrToInt(PosNE->Text)].mc = CountSE->Value;
	}
}
//---------------------------------------------------------------------------
void __fastcall TSF::CountSEKeyPress(TObject *Sender, wchar_t &Key) {
	if (Key == '\r' || !(Key >= '0' && Key <= '9' || Key == '\b'))
		Key = 0;
}
//---------------------------------------------------------------------------
void __fastcall TSF::SaveBClick(TObject *Sender) {
	SaveSettings();
	SF->Close();
}
//---------------------------------------------------------------------------
void __fastcall TSF::FormChBMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (Sender == FormChB) {
		if (FormChB->Checked) {
			t[StrToInt(TrnNE->Text)].form = false;
			FormChB->Checked = true;
		}
		else {
			t[StrToInt(TrnNE->Text)].form = true;
			FormChB->Checked = false;
		}
	}
}
//---------------------------------------------------------------------------
void __fastcall TSF::ApplyBClick(TObject *Sender) {
	SaveSettings();
}
//---------------------------------------------------------------------------
void __fastcall TSF::CancelBClick(TObject *Sender) {
	SF->Close();
}
//---------------------------------------------------------------------------
void __fastcall TSF::DTrnMKeyUp(TObject *Sender, WORD &Key, TShiftState Shift) {
	t[StrToInt(TrnNE->Text)].des = DTrnM->Text;
}
//---------------------------------------------------------------------------
void __fastcall TSF::DPosMKeyUp(TObject *Sender, WORD &Key, TShiftState Shift) {
	p[StrToInt(PosNE->Text)].des = DPosM->Text;
}
//---------------------------------------------------------------------------
void __fastcall TSF::PosChBMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (PosChB->Checked) {
		p[StrToInt(PosNE->Text)].mc = 0;
		CountSE->Enabled = true;
	}
	else {
		p[StrToInt(PosNE->Text)].mc = 100;
		CountSE->Enabled = false;
	}
	CountSE->Value = 0;
}
//---------------------------------------------------------------------------
